const HttpClass = require('./Class/Http')
const storageModule = require('@/storage/index')

module.exports = new HttpClass({
  httpMethod: wx.request,
  baseURL: '',
  header: {
    'content-type': 'application/x-www-form-urlencoded'
  },
  timeout: 1000 * 10,
  method: 'GET',
  interceptors: {
    request(config) {
      // demo: 添加加载loading
      // 配置参数判断 loading 显示
      if (config.isLoading) {
        wx.showLoading()
      }
      // 添加请求头 token
      config.header['token'] = storageModule.getToken()
      return config
    },
    // 支持 Promise
    response(res, error) {
      // demo: 根据配置参数隐藏loading
      const finallyRes = res || error
      const config = finallyRes.config
      if (config.isLoading) {
        wx.hideLoading()
      }
      // 成功回调
      if (res) {
        console.log('request success')
        return res
        /*
          return Promise.resolve( res ) 进入成功回调
          return Promise.reject( res ) 进入失败回调
        */
      } else {
        // 失败回调
        console.log('request error')
        return error
      }
    }
  }
})

